﻿@using System.Net.Http.Headers
@using Tool.Models
@using Tool.Services
@using Tool.Utils
@inject ISnackbar snackbar
@inject TerminalService terminalService
@inject IDialogService dialogService
@inject LoginService loginService
<MudGrid>
	<MudItem xs="3">
		<MudTextField @bind-Value="UserCode" Label="单位编码" Variant="Variant.Text" Adornment="Adornment.Start"
					  AdornmentIcon="@Icons.Material.Filled.LockPerson" Clearable="true" />
	</MudItem>

	<MudItem xs="3" Class="align-content-center">
		<MudButton Class="align-self-center" FullWidth="true" Disabled="@_processing" OnClick="GetTerminalInfo" Variant="Variant.Filled" Color="Color.Primary">
			@if (_processing)
			{
				<MudProgressCircular Class="ms-n1" Indeterminate="true" />
				<MudText Class="ms-2">Processing</MudText>
			}
			else
			{
				<MudText>获取终端信息</MudText>
			}
		</MudButton>
	</MudItem>

	<MudItem xs="3" Class="align-content-center">
		<MudButton Class="align-self-center" FullWidth="true" OnClick="SyncHistoryData" Variant="Variant.Filled" Color="Color.Primary">

			<MudText>同步历史数据/通知云伴侣</MudText>

		</MudButton>
	</MudItem>

	<MudItem xs="12">
		<MudTable Items="@_terminals" @bind-SelectedItems="_selectedTerminals" Dense="true" Hover="true" Bordered="true" Striped="true" MultiSelection="true" CustomHeader="true" CustomFooter="true">
			<ToolBarContent>
				<MudText Typo="Typo.h6">终端信息</MudText>
				<MudSpacer />
			</ToolBarContent>
			<HeaderContent>
				<MudTHeadRow Checkable="true">
					<MudTh>Id</MudTh>
					<MudTh>UserCode</MudTh>
					<MudTh>TerminalSn</MudTh>
					<MudTh>MachineCode</MudTh>
					<MudTh>CheckCode</MudTh>
					<MudTh>ExpirationTime</MudTh>
					<MudTh>CreateTime</MudTh>
					<MudTh>UpdateTime</MudTh>
					<MudTh>IsDeleted</MudTh>
				</MudTHeadRow>
			</HeaderContent>
			<RowTemplate>
				<MudTd DataLabel="Id">@context.Id</MudTd>
				<MudTd DataLabel="Id">@context.UserCode</MudTd>
				<MudTd DataLabel="TerminalSn">@context.TerminalSn</MudTd>
				<MudTd DataLabel="MachineCode">@context.MachineCode</MudTd>
				<MudTd DataLabel="CheckCode">@context.CheckCode</MudTd>
				<MudTd DataLabel="ExpirationTime">@context.ExpirationTime</MudTd>
				<MudTd DataLabel="CreateTime">@context.CreateTime</MudTd>
				<MudTd DataLabel="UpdateTime">@context.UpdateTime</MudTd>
				<MudTd DataLabel="IsDeleted">@context.IsDeleted</MudTd>
			</RowTemplate>
			<FooterContent>
				<MudTFootRow Checkable="true">
					<MudTd colspan="5">Selected: @_selectedTerminals?.Count</MudTd>
				</MudTFootRow>
			</FooterContent>
			<PagerContent>
				<MudTablePager />
			</PagerContent>
		</MudTable>
	</MudItem>
</MudGrid>
@code {
	[Parameter]
	public string UserCode { get; set; }
	[Parameter]
	public string SelectedSite { get; set; }

	[Parameter]
	public string GroupName { get; set; }

	[Parameter]
	public HttpResponseHeaders ResponseHeaders { get; set; }
	public bool _processing = false;

	private List<VoucherTerminalDto> _terminals = new List<VoucherTerminalDto>();
	private HashSet<VoucherTerminalDto> _selectedTerminals = new HashSet<VoucherTerminalDto>();

	protected override async Task OnParametersSetAsync()
	{
		await base.OnParametersSetAsync();
		await GetTerminalInfo();
	}

	private async Task GetTerminalInfo()
	{
		_processing = true;
		try
		{
			if (UserCode.IsBlank())
			{
				return;
			}
			var sql = $"select id,terminal_sn,machine_code,check_code, expiration_time, user_code,create_time,update_time,is_deleted  from t_voucher_terminal where user_code = '{UserCode}' or check_code='{UserCode}'";
			if (SelectedSite != "zwy")
			{
				ResponseHeaders = await loginService.Login(SelectedSite);
			}
			_terminals = await terminalService.GetTerminalInfo(sql, SelectedSite, ResponseHeaders, string.Empty);
		}
		catch (Exception ex)
		{
			snackbar.Add($"Error: {ex.Message}", Severity.Error);
		}
		finally
		{
			_processing = false;
		}
	}

	private async Task SyncHistoryData()
	{
		if (_selectedTerminals.Count == 0)
		{
			snackbar.Add("请选择终端信息", Severity.Error);
			return;
		}

		var options = new DialogOptions
		{
			FullWidth = true,
			MaxWidth = MaxWidth.Medium,
			CloseOnEscapeKey = true,
			Position = DialogPosition.TopCenter
			// FullScreen = true
		};
		await dialogService.ShowAsync<SyncHistoryDataDialog>("同步历史数据", new DialogParameters {
			{ "UserCode", _selectedTerminals.FirstOrDefault()?.UserCode },
			{ "GroupName", GroupName },
			{ "CheckCode", _selectedTerminals.FirstOrDefault()?.CheckCode },
			{ "MachineCode", _selectedTerminals.FirstOrDefault()?.MachineCode },
		}, options);
	}

}
